/* cgisvr.h : dgnsc cgi server
 * Copyright (C) 2009-2011 drangon <drangon.zhou@gmail.com>
 * 2011-03
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_DGNSC_CGISVR_H
#define INCLUDED_DGNSC_CGISVR_H

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

// <1> type
typedef struct dgn_cgisvr_st * dgn_cgisvr_t;
typedef struct dgn_cgi_st * dgn_cgi_t;
typedef int ( * dgn_cgi_callback_func_t )( dgn_cgi_t cgi );

// <2> cgi server
enum {
	DGN_CGISVR_TYPE_UNKNOWN = 0,
	DGN_CGISVR_TYPE_FASTCGI,
	DGN_CGISVR_TYPE_SCGI,
};

dgn_cgisvr_t dgn_cgisvr_init( int cgi_type, dgn_cgi_callback_func_t cb_func, const char * host, int port, int thread_num );
void dgn_cgisvr_fini( dgn_cgisvr_t cs );
int dgn_cgisvr_start( dgn_cgisvr_t cs );
int dgn_cgisvr_stop( dgn_cgisvr_t cs ); // must not call in callback

// <3> used in cgi callback function
// <3.1> get request info from dgn_cgi_t
const char * dgn_cgi_get_env( dgn_cgi_t cgi, const char * header );
int dgn_cgi_get_body_len( dgn_cgi_t cgi );
const char * dgn_cgi_get_body( dgn_cgi_t cgi );

// helper function, request detail
const char * dgn_cgi_get_script_name( dgn_cgi_t cgi );
const char * dgn_cgi_get_query_string( dgn_cgi_t cgi );
const char * dgn_cgi_get_cookie( dgn_cgi_t cgi, const char * name );
const char * dgn_cgi_get_param( dgn_cgi_t cgi, const char * name );

// <3.2> send reply to dgn_cgi_t
// reply header
int dgn_cgi_reply_status( dgn_cgi_t cgi, int code, const char * reason ); // can omit default 200 ok
int dgn_cgi_reply_content_type( dgn_cgi_t cgi, const char * content_type );
int dgn_cgi_reply_location( dgn_cgi_t cgi, const char * location );
int dgn_cgi_reply_header( dgn_cgi_t cgi, const char * name, const char * value );
// age == 0 : remove; age < 0 : valid until browser closed
int dgn_cgi_reply_cookie( dgn_cgi_t cgi, const char * name, const char * value, int age_sec );

// reply body
int dgn_cgi_reply_body( dgn_cgi_t cgi, const char * body, int len );
int dgn_cgi_reply_body_fmt( dgn_cgi_t cgi, const char * fmt, ... ) __attribute__((format (printf,2,3) ));

// raw reply, not used in common
int dgn_cgi_reply( dgn_cgi_t cgi, const char * data, int len );

#ifdef __cplusplus
}
#endif // __cplusplus

#endif // INCLUDED_DGNSC_CGISVR_H

